/*
 *  $TEAM_NAME Space Trader
 */
package models;


import java.io.File;
import java.io.IOException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlRootElement;

/**
 * The game class represents a game within space trader 
 * @author bradley
 * @version 1.0
 */
@XmlRootElement
public class Game {
	/**
	 * field
	 */
	private Player player;
    
	/**
	 * field
	 */
	private Galaxy galaxy;
    
	/**
	 * field
	 */
	private Difficulty difficulty;
	
	/**
	 * No arg constructor for game
	 */
	public Game(){
		this.player = null;
	}
    
    /**
     * The default constructor for game
     * @param galaxy - the galaxy 
     * @param player - the player
     * @param difficulty - the difficulty
     */
    public Game(Galaxy galaxy, Player player, Difficulty difficulty) {
    	this.galaxy = galaxy;
    	this.player = player;
    	//this.setGalaxy(galaxy);
    	//this.setPlayer(player);
    	this.setDifficulty(difficulty);
    }

    /**
     * Getter for the player
     * @return - gets the player
     */
    public Player getPlayer() {
        return player;
    }
    
    /**
     * Setter for player
     * @param player
     */
    public void setPlayer(Player player) {
    	this.player = player;
    }
    
    /**
     * getter for the galaxy
     * @return - gets the galaxy
     */
    public Galaxy getGalaxy() {
        return galaxy;
    }

    /**
     * Setter for galaxy
     * @param galaxy
     */
    public void setGalaxy(Galaxy galaxy) {
    	this.galaxy = galaxy;
    }
    
    /**
     * Setter for difficulty
     * @param difficulty
     */
    public final void setDifficulty(Difficulty difficulty) {
    	this.difficulty = difficulty;
    }
    
    /**
     * Getter for difficulty
     * @return difficulty
     */
    public Difficulty getDifficulty() {
    	return this.difficulty;
    }
    
    /**
     * Gets the string representation of the game
     * @return - the string
     */
    public String toString() {
        String str = "Game:";

        str += "\nDifficulty:\t" + difficulty.toString();
        str += "\n" + player.toString();
        str += "\n" + galaxy.toString();

        return str;
    }
    
    /**
     * Saves the game
     * @param game - the game to save
     */
    public static void save(Game game) {
		final String fileName = "Saves/Save1.xml";
		final File file = new File(fileName);
		try {
			file.createNewFile();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		
		try{
			final JAXBContext jaxbcontext = JAXBContext.newInstance(Game.class);
			final Marshaller jaxbMarshaller = jaxbcontext.createMarshaller();
			jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
			jaxbMarshaller.marshal(game, file);
			
		} catch (JAXBException e) {
			e.printStackTrace();
		}
		
	}

    /**
     * Loads a saved game
     * @return - the game model
     */
	public static Game load() {
		final String fileName = "Saves/Save1.xml";
		final File file = new File(fileName);
		try{
			final JAXBContext jaxbContext = JAXBContext.newInstance(Game.class);
			final Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
			
			final Game game = (Game) jaxbUnmarshaller.unmarshal(file);
			return game;
		} catch (JAXBException e) {
			System.err.println(e);
		}
		return null;
	}
}
